<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/header.php");
?>

<section>
<hgroup>
<h1>Email-adres veranderen</h1>
</hgroup>

<?php if(!isset($_POST['Submit'])) { ?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

  <table style="margin-left: 25px">
    <tr>
      <td><p>Huidig email-adres:</p></td>
      <td><input name="huidigemail" type="text" size="35" /></td>
    </tr>
    <tr>
      <td><p>Nieuw email-adres:</p></td>
      <td><input name="email" type="text" size="35" onkeyup="passwordStrength(this.value)" /></td>
    </tr>
    <tr>
      <td><p>Nieuw email-adres herhalen:</p></td>
      <td><input name="email2" type="text" size="35"></td>
    </tr>
    <tr>
      <td colspan="2"><input type="submit" name="Submit" value="Veranderen"></td>
    </tr>
  </table>
</form>
<?php } else {
    $error = array();
    $checkp = mysql_result(mysql_query("SELECT COUNT(*) FROM user WHERE UserName='".$_SESSION['username']."' AND Email='".$_POST['huidigemail']."'"),0);

    if($_POST['email'] != "" && $checkp == 0) {
        $error[] = '<p><span style="color:red">Het huidige email-adres is niet correct.</span></p>';
    }

    if(!check_email($_POST['email'])) {
		$error[] = '<p><span style="color:red">Er is geen geldig email-adres ingevuld.</span></p>';
	}
    
    if($_POST['email'] == "" || $_POST['email'] != $_POST['email2']) {
        $error[] = '<p><span style="color:red">Er is geen nieuw email-adres ingevuld of de 2 email-adressen komen niet overeen.</span></p>';
    }

    $errors = sizeof($error); // aantal errors tellen
    if($errors != 0) { // Er is minstens 1 error
        echo '<p><span style="color:red">Uw email-adres kon niet verandert worden omwille van de volgende reden(en):</span></p>';
        for($i = 0; $i < $errors; $i++) {
            echo $error[$i];
    }

    } else {	
        mysql_query("UPDATE user SET Email = '".$_POST['email']."' WHERE Id ='".$_SESSION['userid']."'");
        echo "<p>Je email-adres is veranderd.</p>";
    }
}
?>


</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>